home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / c-lang / hex.lha / hex.c < prev    next >
C/C++ Source or Header  |  1996-05-07  |  1KB  |  79 lines

  1. /*
  2.  * $VER: hex.c 1.0 (7.5.96)
  3.  *
  4.  * ©1996 António Manuel Santos
  5.  * 
  6.  * EMAIL: L38058@ALFA.IST.UTL.PT
  7.  *
  8.  * SNAIL:  António Manuel Santos
  9.  *         Rua do Zaire,5 1ºdto
  10.  *         1170 Lisbon, Portugal
  11.  *
  12.  *
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23.  
  24. #define INVALID_CHAR    '.'
  25. #define STDIN_FD        0
  26.  
  27. int input = -1;
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.     if (argc <2) {
  32.         input = STDIN_FD;
  33.     } else {
  34.         input = open(argv[1], O_RDONLY);
  35.     }
  36.     
  37.     if (input != -1) {
  38.         int k;
  39.         char buff[16];
  40.         int offset = 0;
  41.         
  42.         while ( (k = read(input, buff, 16)) > 0 ) {
  43.             int j;
  44.             
  45.             printf("\033[1m%08lx:\033[22m  ", offset);
  46.             
  47.             for (j = 0; j < k; j++) {
  48.                 printf("%02x", (unsigned char)buff[j]);
  49.                 if (!((j+1) % 4))
  50.                     printf(" ");
  51.             }
  52.                 
  53.             for (j = 0; j < (17-k); j++) {
  54.                 printf("  ");
  55.                 if (!((j+1) % 4))
  56.                     printf(" ");
  57.             }
  58.             
  59.             
  60.             for (j = 0; j < k; j++) {
  61.                 printf("%c", ((buff[j] >= ' ') && (buff[j] <= 'z')) ? buff[j] : INVALID_CHAR);
  62.             }
  63.             
  64.             if (k == 16)
  65.                 printf("\n");
  66.             else if (k > 0) {
  67.                 printf("\n");
  68.             }
  69.             
  70.             offset += 4;
  71.         }
  72.         
  73.         if (input != STDIN_FD)
  74.             close(input);
  75.     }
  76.     
  77.     exit(0);
  78. }
  79.